import java.sql.*;

public class FixBusinessMonitorComponentPaths {
    private static final String DB_URL = "jdbc:mysql://db.test1.wcsbapp.com:3306/ChainUp";
    private static final String DB_USER = "admin";
    private static final String DB_PASSWORD = "0YvoGUzwhumnf2KGQvjN";

    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
                
                System.out.println("=== 🔧 修复业务监控组件路径配置 ===");
                System.out.println("将后端菜单配置修改为匹配现有前端组件路径");
                
                // 1. 查看当前配置
                showCurrentConfiguration(conn);
                
                // 2. 修复路径配置
                fixComponentPaths(conn);
                
                // 3. 验证修复结果
                verifyFixedConfiguration(conn);
                
                System.out.println("=== ✅ 组件路径修复完成 ===");
                
            }
        } catch (Exception e) {
            System.err.println("修复路径时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static void showCurrentConfiguration(Connection conn) throws SQLException {
        System.out.println("\n1. 📋 当前业务监控菜单配置:");
        
        String sql = "SELECT menu_id, menu_name, component FROM sys_menu " +
                    "WHERE path IN ('realtimeKline', 'quotes', 'orderbook', 'lasttrade', 'ticker', 'transaction') " +
                    "AND menu_type = 'C' ORDER BY menu_id";
        
        try (PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            
            System.out.printf("   %-6s %-15s %-50s%n", "ID", "菜单名称", "当前组件路径");
            System.out.println("   " + "-".repeat(75));
            
            while (rs.next()) {
                int menuId = rs.getInt("menu_id");
                String menuName = rs.getString("menu_name");
                String component = rs.getString("component");
                
                System.out.printf("   %-6d %-15s %-50s%n", menuId, menuName, component);
            }
        }
    }
    
    private static void fixComponentPaths(Connection conn) throws SQLException {
        System.out.println("\n2. 🔧 修复组件路径配置...");
        
        // 定义路径映射：菜单ID -> 正确的前端组件路径
        String[][] pathMappings = {
            // 现货监控 - 修改为匹配现有前端文件路径
            {"520", "realtimeKline/realtimeKlinebak/index"},      // 实时K线
            {"521", "spot_openapi_quotesticker/spot_openapi_quotesticker_bak/index"},  // 行情数据
            {"522", "spot_openapi_orderbook/spot_openapi_orderbook_bak/index"},        // 订单簿
            {"523", "spotrealtimelastesttrade/spotrealtimelastesttradebak/index"},      // 最新交易
            {"524", "spotrealtimeticker/spotrealtimetickerbak/index"},                 // Ticker数据
            {"525", "spotrealtimetransaction/spotrealtimetransactionbak/index"},       // 交易记录
            
            // OpenAPI监控 - 修改为匹配现有前端文件路径  
            {"526", "spot_openapi_quotesticker/spot_openapi_quotesticker_bak/index"},  // OpenAPI行情
            {"527", "spot_openapi_orderbook/spot_openapi_orderbook_bak/index"}         // OpenAPI订单簿
        };
        
        for (String[] mapping : pathMappings) {
            String menuId = mapping[0];
            String newComponent = mapping[1];
            
            String updateSql = "UPDATE sys_menu SET component = ? WHERE menu_id = ?";
            
            try (PreparedStatement stmt = conn.prepareStatement(updateSql)) {
                stmt.setString(1, newComponent);
                stmt.setInt(2, Integer.parseInt(menuId));
                
                int updated = stmt.executeUpdate();
                if (updated > 0) {
                    System.out.printf("   ✅ 菜单ID %s: 更新组件路径为 %s%n", menuId, newComponent);
                } else {
                    System.out.printf("   ⚠️ 菜单ID %s: 未找到或更新失败%n", menuId);
                }
            }
        }
    }
    
    private static void verifyFixedConfiguration(Connection conn) throws SQLException {
        System.out.println("\n3. ✅ 验证修复后的配置:");
        
        String sql = "SELECT menu_id, menu_name, component FROM sys_menu " +
                    "WHERE menu_id IN (520, 521, 522, 523, 524, 525, 526, 527) " +
                    "ORDER BY menu_id";
        
        try (PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            
            System.out.printf("   %-6s %-15s %-60s%n", "ID", "菜单名称", "修复后组件路径");
            System.out.println("   " + "-".repeat(85));
            
            while (rs.next()) {
                int menuId = rs.getInt("menu_id");
                String menuName = rs.getString("menu_name");
                String component = rs.getString("component");
                
                System.out.printf("   %-6d %-15s %-60s%n", menuId, menuName, component);
                
                // 检查对应的前端文件是否存在
                String frontendPath = "../ChainUp_Monitor_Front/src/views/" + component + ".vue";
                java.io.File file = new java.io.File(frontendPath);
                if (file.exists()) {
                    System.out.printf("         ✅ 前端组件文件存在%n");
                } else {
                    System.out.printf("         ❌ 前端组件文件不存在: %s%n", frontendPath);
                }
            }
        }
        
        System.out.println("\n📋 修复说明:");
        System.out.println("   • 已将后端菜单配置修改为匹配现有前端组件路径");
        System.out.println("   • 无需修改前端代码，直接使用现有组件");
        System.out.println("   • 业务监控页面现在应该能正常加载并发送API请求");
        System.out.println("   • 请刷新浏览器页面验证修复效果");
    }
}